<template>
  <div class="alarm-map-wrap"></div>
</template>

<script>
import AMap from 'AMap'
import config from '../../../config'
import Alarm from '../../../../src/assets/scripts/plugins/alarm'

let alarmMap
export default {
  name: 'alarm',
  props: ['mapList', 'curMarker'],
  data () {
    return {
      mapStyle: config.mapStyle
    }
  },
  mounted () {
    let _this = this
    // let gWidth = this.$base.getDocumentWidth() - this.$base.getConst('common', 'sidebarWidth')
    // let gHeight = this.$base.getDocumentHeight() - this.$base.getConst('common', 'headerHeight')
    alarmMap = new Alarm({
      target: '.alarm-map-wrap',
      w: '100%',
      h: '100%',
      mapStyle: _this.mapStyle, // 设置地图的显示样式
      AMap: AMap,
      popWindowTemp: _this.popWindowTemp,
      data: []
    })
    alarmMap.init()
    this.$store.commit('setSupMap', alarmMap)
  },
  watch: {
    'mapList': function (res) {
      if (!res || res.length === 0) {
        return
      }
      for (let i = 0, len = res.length; i < len; i++) {
        if (i === 0) {
          alarmMap.addDot(res[i], true)
        } else {
          alarmMap.addDot(res[i])
        }
      }
    }
  },
  methods: {
    popWindowTemp (data) {
      // 地图嵌入的信息窗文字
      let sContent = ''
      sContent += '<div class="mapStyle">'
      sContent += '<h2>报警详情</h2>'
      sContent += '<ul>'
      sContent += '<li class="clearfix"><div class="mapStyle-left">编号:</div><div class="mapStyle-right">' + data.eventId + '</div></li>'
      sContent += '<li class="clearfix"><div class="mapStyle-left">场景:</div><div class="mapStyle-right">' + data.moduleName + '</div></li>'

      if (Object.prototype.toString.call(data.data) !== '[object Array]') {
        // val = JSON.parse(data.data)
      }

      if (data.eventValue) {
        sContent += '<li class="clearfix"><div class="mapStyle-left">告警类型:</div><div class="mapStyle-right">' + data.eventValue + '</div></li>'
      }

      let eventAddress = data.eventContent || '-'
      sContent += '<li class="clearfix"><div class="mapStyle-left">报警时间:</div><div class="mapStyle-right">' + data.eventTime + '</div></li>'
      sContent += '<li class="clearfix"><div class="mapStyle-left">地址:</div><div class="mapStyle-right">' + eventAddress + '</div></li>'
      // sContent += '<li class="clearfix"><div class="mapStyle-left">照片:</div><div class="mapStyle-right"><div class="myStyle-img"><img src="public/images/pic_demo.jpg"></div><div class="myStyle-img"><img src="public/images/pic_demo.jpg"></div><div class="myStyle-img"><img src="public/images/pic_demo.jpg"></div></div></li>';
      sContent += '</ul>'
      // sContent += '<a href="process.html?id=' + data.id + '">查看进度</a>'
      if (data.videoUrls) {
        for (let url in data.videoUrls) {
          sContent += '<a data-id="' + url + '" class="mapStyle-video" style="margin-top:1px">查看监控</a>'
        }
      }
      sContent += '</div>'
      return sContent
    }
  }
}
</script>

<style>
  .alarm-map-wrap{
    height: 100%;
  }
  .alarm-map-wrap .mapStyle h2 {
    font-size: 14px;
    color: #f90;
    text-align: center;
  }

  .alarm-map-wrap .mapStyle li {
    list-style: none;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 12px;
    line-height: 30px;
    padding: 2px 0;
  }

  .alarm-map-wrap .mapStyle-left {
    float: left;
    text-align: left;
    color: #9FACC3;
  }

  .alarm-map-wrap .mapStyle-right {
    float: right;
    text-align: right;
    color: #fff;
    width: 150px;
  }

  .alarm-map-wrap .mapStyle-right .high {
    color: #ff6600;
  }

  .alarm-map-wrap .myStyle-img {
    float: left;
    margin-left: 5px;
    width: 56px;
    height: 42px;
    overflow: hidden;
  }

  .alarm-map-wrap .myStyle-img img {
    width: 56px;
  }

  .alarm-map-wrap .mapStyle a {
    display: block;
    width: 245px;
    height: 40px;
    line-height: 40px;
    background-color: #ff6600;
    margin: 0 auto;
    text-align: center;
    color: #fff;
    text-decoration: none;
    cursor: pointer;
  }

  .alarm-map-wrap .mapStyle h2 {
    margin: 0;
  }
</style>
